﻿namespace Admin.Pages
{
    using System;
    using System.IO;
    using System.Linq;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;

    using BlogEngine.Core;

    using Resources;

    using Page = System.Web.UI.Page;
    using App_Code;
    using BlogEngine.Core.Providers;

public partial class Admin.AdminMasterPage: Page, ICallbackEventHandler
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            RefreshDropDownGalleries();
            //foreach (string item in Directory.GetDirectories(Server.MapPath("/gallery")))
            //{
            //    DirectoryInfo di = new DirectoryInfo(item);
            //    dropGallery.Items.Add(di.Name);
            //    dropGallery_SelectedIndexChanged(null, null);
            //}
        }
        //dropGallery.Items.Clear();
    }

    protected string[] GenerateGallery(string path)
    {
        string fullPath = "/gallery/" + path;
        string[] result = new string[System.IO.Directory.GetFiles(MapPath(fullPath), "*.jpg").Length];
        int i=0;
        foreach (string item in System.IO.Directory.GetFiles(MapPath(fullPath), "*.jpg"))
        {
            FileInfo fi = new FileInfo(item);
            result[i] = fullPath + "/thumbs/" + fi.Name;
            i++;
        }

        return result; //Directory.GetFiles(MapPath(fullPath), "*.jpg");
    }
    protected void dropGallery_SelectedIndexChanged(object sender, EventArgs e)
    {
        repGallery.DataSource = GenerateGallery(dropGallery.SelectedItem.Text);
        repGallery.DataBind();
    }
    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        //txtNew.Text = Server.MapPath("/gallery/") + txtNew.Text;
        Directory.CreateDirectory(Server.MapPath("/gallery/") + txtNew.Text);
        Directory.CreateDirectory(Server.MapPath("/gallery/") + txtNew.Text + "/thumbs");

        RefreshDropDownGalleries();

        dropGallery.SelectedIndex = dropGallery.Items.IndexOf(dropGallery.Items.FindByValue(txtNew.Text));
        dropGallery_SelectedIndexChanged(null, null);
    }


    protected void lnkDeleteGallery_Click(object sender, EventArgs e)
    {
        Directory.Delete(Server.MapPath("/gallery/") + dropGallery.SelectedItem.Text,true);
        RefreshDropDownGalleries();
    }

    protected void repGallery_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "delete")
        {
            string path = ((System.Web.UI.WebControls.Image)e.Item.FindControl("img")).ImageUrl;
            File.Delete(Server.MapPath(path));
            File.Delete(Server.MapPath(path.Replace("thumbs/","")));

            dropGallery_SelectedIndexChanged(null, null);
        }
    }


    protected void btnRename_Click(object sender, EventArgs e)
    {
        Directory.Move(Server.MapPath("/gallery/") + dropGallery.SelectedItem.Text, Server.MapPath("/gallery/") + txtRename.Text);
        RefreshDropDownGalleries();
        dropGallery_SelectedIndexChanged(null, null);
    }

    protected void btnAddNewImg_Click(object sender, EventArgs e)
    {
        //
        string[] file = uploadImg.PostedFile.FileName.Split(new char[] {'.'});
        //btnAddNewImg.Text = file[file.Length-1] + " - " + file.Length;

        string imgFileName = string.Format("img{0:yyyy-MM-dd_hh-mm-ss-tt}." + file[file.Length - 1], DateTime.Now);

        if (uploadImg.PostedFile.ContentType.ToLower().Contains("image"))
        {
            using (System.Drawing.Image img = System.Drawing.Image.FromStream(uploadImg.PostedFile.InputStream))
            {
                System.Drawing.Image imgThumb = resizeImage(img, new Size(0, 62));
                imgThumb.Save(Server.MapPath("/gallery/") + dropGallery.SelectedItem.Text + @"\thumbs\" + imgFileName);
                imgThumb.Dispose();

                System.Drawing.Image imgMain = resizeImage(img, new Size(600, 450));
                imgMain.Save(Server.MapPath("/gallery/") + dropGallery.SelectedItem.Text + @"\" + imgFileName);
                imgMain.Dispose();
            }

             dropGallery_SelectedIndexChanged(null, null);

        }

    }

    private string RemoveFullPath(string path)
    {
        string[] file = path.Split(new char[] { '/' });
        return file[file.Length - 1];
    }

    private void RefreshDropDownGalleries()
    {
        dropGallery.Items.Clear();
        foreach (string item in Directory.GetDirectories(Server.MapPath("/gallery")))
        {
            DirectoryInfo di = new DirectoryInfo(item);
            dropGallery.Items.Add(di.Name);
            dropGallery_SelectedIndexChanged(null, null);
        }
    }
    private static System.Drawing.Image resizeImage(System.Drawing.Image imgToResize, Size size)
    {
        int sourceWidth = imgToResize.Width;
        int sourceHeight = imgToResize.Height;

        float nPercent = 0;
        float nPercentW = 0;
        float nPercentH = 0;

        nPercentW = ((float)size.Width / (float)sourceWidth);
        nPercentH = ((float)size.Height / (float)sourceHeight);

        //if (nPercentH < nPercentW)
        //    nPercent = nPercentH;
        //else
        //    nPercent = nPercentW;

        if (sourceWidth < sourceHeight)
            nPercent = nPercentH;
        else
            nPercent = nPercentW;

        if (size.Width == 0)
            nPercent = nPercentH;

        int destWidth = (int)(sourceWidth * nPercent);
        int destHeight = (int)(sourceHeight * nPercent);

        Bitmap b = new Bitmap(destWidth, destHeight);
        Graphics g = Graphics.FromImage((System.Drawing.Image)b);
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

        g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
        g.Dispose();

        return (System.Drawing.Image)b;
    }



}
